﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.BL.CCFlow.EV;
using BEGDC.CC.BL.Common.Query;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web.Workflow
{
	/// <summary>
	/// Summary description for EVTaskList.
	/// </summary>
	public class EVTaskList : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DropDownList ddlDept;
		protected System.Web.UI.WebControls.DropDownList ddlAppType;
		protected System.Web.UI.WebControls.ImageButton ibtnApprove;
		protected System.Web.UI.WebControls.ImageButton ibtnReject;
		protected System.Web.UI.WebControls.ImageButton ibtnExport;
		protected System.Web.UI.WebControls.DataGrid dtGridEV;
		protected System.Web.UI.WebControls.TextBox txtApplication;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if( !Page.IsPostBack )
			{
				InitDataGrid();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.ibtnApprove.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnApprove_Click);
			this.ibtnReject.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnReject_Click);
			this.dtGridEV.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dtGridEV_ItemDataBound);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void InitDataGrid()
		{
			IEV EVService = AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.EVService] as IEV;
			dtGridEV.DataSource = EVService.GetEVTasksByEmployee(((BEGDC.CC.BL.Workflow.CCIdentity)HttpContext.Current.User.Identity).UserInfo);
			dtGridEV.DataBind();
		}

		

		private void dtGridEV_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				DropDownList ddlStaff = e.Item.Cells[e.Item.Cells.Count-1].FindControl("ddlCODStaff") as DropDownList;
				if( ddlStaff == null )
					return;
				else
					ddlStaff.Items.Add(new ListItem("Joe", "Joe"));

				HyperLink hl = e.Item.Cells[7].FindControl("hlEdit") as HyperLink;
				if( hl != null )
				{
					int appId = Convert.ToInt32( e.Item.Cells[0].Text );
					hl.Target="_blank";
					hl.NavigateUrl = "WorkflowExecution.aspx?PDType=EV&AppID="+appId.ToString();
				}
			}
		}

		private void ibtnApprove_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			this.CODManagerAssign();
		}
		
		private void CATPSODriecotrApprove()
		{
			IEV evService = AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.EVService] as IEV;
			evService.ApproveEVRequest(this.BuildApp2Action(), null);
		}

		private void CODManagerAssign()
		{
			IEV evService = AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.EVService] as IEV;
			//evService.AssignEVRequest( this.BuildAppMember2Action(), null );

		}

		private IList BuildApp2Action()
		{
			IList appList = new ArrayList();
			IQuery queryService = AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as IQuery;
			foreach(System.Web.UI.WebControls.DataGridItem item in dtGridEV.Items )
			{
				if( item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem )
				{
					CheckBox chkBox = item.Cells[1].FindControl("chkEVSelect") as CheckBox;
					if( chkBox != null && chkBox.Checked)
					{
						int appId = Convert.ToInt32( item.Cells[0].Text );					
						Entity.Application app = queryService.GetAppById( appId );
						appList.Add( app );
					}
				}
			}

			return appList;
		}

		private Hashtable BuildAppMember2Action()
		{
			Hashtable hashtable = new Hashtable();
			IQuery queryService = AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as IQuery;
			foreach(System.Web.UI.WebControls.DataGridItem item in dtGridEV.Items )
			{
				if( item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem )
				{
					CheckBox chkBox = item.Cells[1].FindControl("chkEVSelect") as CheckBox;
					if( chkBox != null && chkBox.Checked)
					{
						int appId = Convert.ToInt32( item.Cells[0].Text );					
						Entity.Application app = queryService.GetAppById( appId );
						hashtable.Add( app, null );
					}
				}
			}

			return hashtable;
		}

		private void ibtnReject_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			this.CATPSODriecotrApprove();
		}

	}
}
